const child_process = require('child_process'),
    exec = child_process.exec,
    execSync = child_process.execSync,
    path = process.cwd();

let prog = null;

function add(step1,step2) {
    const cmd = `git add -A`;
    console.log(cmd);
    const result = exec(cmd, (err, stdout, stderr) => {
        if (err) {
            console.error(err);
        }
    });
    result.stdout.pipe(process.stdout); 
    result.stderr.pipe(process.stderr); 
    result.on('exit', (code, signal) => {
        step1&&step1(step2);
    });
}

function commit(step1) {
    const cmd = `git commit -m "${prog.mesg||'release'}"`;
    console.log(cmd);
    const result = exec(cmd, (err, stdout, stderr) => {
        if (err) {
            console.error(err);
        }
    });
    result.stderr.pipe(process.stderr); 
    result.stdout.pipe(process.stdout); 
    result.on('exit', (code, signal) => {
        step1&&step1();
    });
}


function push(remote,branch) {
    let cmd,result;
    if(!remote||!branch){
        cmd = 'git branch -vv';
        console.log(cmd);
        result = exec(cmd, (err, stdout, stderr) => {
            if (err) {
                console.error(err);
                return;
            }
            let _b = stdout.split(/\s+/)[1]||'master';
            console.log(`branch:${_b}`);
            push('origin',_b);
        });
        return;
    }
    cmd = `git push ${remote} ${branch}`;
    result = exec(cmd, (err, stdout, stderr) => {
        if (err) {
            console.error(err);
        } 
    });
    console.log(cmd);
    result.stdout.pipe(process.stdout); 
    result.stderr.pipe(process.stderr); 
    result.on('exit', (code, signal) => {

    });
}

function execCmd(str) {
    console.log(str);
    const result = exec(str, (err, stdout, stderr) => {
        if (err) {
            console.error(err);
        } 
    });
    result.stdout.pipe(process.stdout); 
    result.stderr.pipe(process.stderr); 
    result.on('exit', (code, signal) => {

    });
}

function checkoutBranch(){
    let branch = prog.args[0] || '';
    const cmd = `git checkout ${branch}`;
    console.log(cmd);
    const result = exec(cmd, (err, stdout, stderr) => {
        if (err) {
            console.error(err);
        } 
    });
    result.stdout.pipe(process.stdout); 
    result.stderr.pipe(process.stderr); 
    result.on('exit', (code, signal) => {

    });
}

function checkFile(){
    let fileName = prog.args[0] || '';
    let cmd = `git check-ignore -v ${fileName}`;
    console.log(cmd);
    const result = exec(cmd, (err, stdout, stderr) => {
        if (err) {
            console.error(err);
        } 
    });
    result.stdout.pipe(process.stdout); 
    result.stderr.pipe(process.stderr); 
    result.on('exit', (code, signal) => {

    });
}

//   .option('ss','status -s')
//   .option('rv','remote -v')
//   .option('b','branch -a')
//   .option('out','checkout branchName')


module.exports = function() {
    prog = arguments[0];
    prog = prog||[];
    if (prog.acp) {
        add(commit,function(){
            push(prog.args[0],prog.args[1]);
        });
    }else if(prog.ac){
        add(commit);
    }else if(prog.ss){
        execCmd('git status -s');
    }else if(prog.rv){
        execCmd('git remote -v');
    }else if(prog.ba){
        execCmd('git branch -a');
    }else if(prog.co){
        checkoutBranch();
    }else if(prog.cf){
        checkFile();
    }
}